package com.lemner.www.hongyu.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.lemner.www.hongyu.Adapter.UndistributedorderAdapter;
import com.lemner.www.hongyu.Bean.UndistributedOrderbean;
import com.lemner.www.hongyu.Bean.Userbean;
import com.lemner.www.hongyu.R;
import com.lemner.www.hongyu.Service.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UndistributedOrderActivity extends AppCompatActivity {
    private RecyclerView rv_undistributed;
    private RecyclerView.LayoutManager layoutManager;
    private UndistributedorderAdapter adapter;
    private UndistributedOrderbean.DataBean bean;
    private Retrofit retrofit;
    private static String BASEURL="http://www.hongyudasha.com/propertysystem/";
    private Service service;
    private Userbean.DataBean date;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_undistributed_order);
        Intent intent=getIntent();
        date= (Userbean.DataBean) intent.getSerializableExtra("date");
        initRecyclerView();
        GetDate();
    }

    private void initRecyclerView() {
        rv_undistributed = (RecyclerView)findViewById(R.id.rv_undistributed);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(this);
        // 设置布局管理器
        rv_undistributed.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rv_undistributed.setHasFixedSize(true);
        adapter = new UndistributedorderAdapter(this);
        rv_undistributed.setAdapter(adapter);
    }
//接单
    public void GetDate(){
         final int worker_id=date.getUser().getWorker_id();
         int []id=new int[]{date.getUser().getCommunity_data().get(0).getCommunity_id()};
          final String ids=Arrays.toString(id);
        retrofit=new  Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
         service=retrofit.create(Service.class);
        Call<UndistributedOrderbean> call=service.getUndistributedOrder(ids,worker_id);
        call.enqueue(new Callback<UndistributedOrderbean>() {
            @Override
            public void onResponse(Call<UndistributedOrderbean> call, Response<UndistributedOrderbean> response) {
                bean=response.body().getData();
                adapter.setList(bean);
                adapter.setWorkerid(worker_id);
                adapter.setIds(ids);
            }
            @Override
            public void onFailure(Call<UndistributedOrderbean> call, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });
    }
}
